package GUI.Dialogs;

import Files.Project;
import GUI.Components.BButton;
import GUI.Components.CancelButton;
import General.CurrentData;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author David
 */
public class CreateProjectDialog extends BasicDialog
{

    private BButton okButton = new BButton("OK");
    private CancelButton cancelButton;
    private JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    private JFileChooser jfc = new JFileChooser();
    private JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    private JTextField nameField = new JTextField("Weeee", 30);

    /**
     *
     */
    public CreateProjectDialog()
    {
        nameField.addKeyListener(new KeyListener()
        {
            public void keyTyped(KeyEvent e)
            {
            }

            public void keyPressed(KeyEvent e)
            {
            }

            public void keyReleased(KeyEvent e)
            {
                okButton.setEnabled(nameField.getText().length() != 0);
                if (e.getKeyCode() == KeyEvent.VK_ENTER)
                {
                    confirm();
                }
            }
        });
        okButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                confirm();
            }
        });
        setTitle("Create Scene");
        cancelButton = new CancelButton(this);
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);
        namePanel.add(new JLabel("Scene Name:"));
        namePanel.add(nameField);
        jfc.setMultiSelectionEnabled(false);
        jfc.setControlButtonsAreShown(false);
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        add(namePanel, BorderLayout.NORTH);
        add(jfc, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
        pack();
        setLocationRelativeTo(null);
        jfc.remove(jfc.getComponent(3));
        setVisible(true);
    }

    private void confirm()
    {
        Project project = new Project(jfc.getCurrentDirectory().getAbsolutePath(), nameField.getText());
        project.create();
        CurrentData.setProject(project);
        CurrentData.initProjectScene();
        CurrentData.setAssetBrowserDialog(new AssetBrowserDialog());
        CreateProjectDialog.this.dispose();
    }
}
